#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import img_compare as ic

similar_rate=0.9
def image_callback(msg):
    # 将ROS图像消息转换为OpenCV图像格式
    result=""
    bridge = CvBridge()
    cv_image = bridge.imgmsg_to_cv2(msg, "bgr8")

    # 在窗口中显示图像
    cv2.imshow("Camera Image", cv_image)
    key=cv2.waitKey(1)

    #识别字母
    if(ic.compare_images(cv_image,ic.imgA_1)>similar_rate or ic.compare_images(cv_image,ic.imgA_2)>similar_rate):
        result="A"
    elif (ic.compare_images(cv_image,ic.imgB_1)>similar_rate or ic.compare_images(cv_image,ic.imgB_2)>similar_rate):
        result="B"
    elif (ic.compare_images(cv_image,ic.imgC_1)>similar_rate or ic.compare_images(cv_image,ic.imgC_2)>similar_rate):
        result="C"
    elif (ic.compare_images(cv_image,ic.imgD_1)>similar_rate or ic.compare_images(cv_image,ic.imgD_2)>similar_rate):
        result="D"
    if result!="":
        rospy.loginfo("识别到字母：%s"  % result)
    # if key == ord('s'):  # 按下"s"键（ASCII值为115）
        #rospy.loginfo("Image saved!")
        # 退出订阅者
        #  rospy.signal_shutdown("Exiting subscriber")

def main():
    rospy.init_node("image_listener")
    image_topic = "/camera/color/image_raw"  # 替换为实际的相机话题名

    # 创建一个图像订阅者，指定回调函数
    rospy.Subscriber(image_topic, Image, image_callback)

    # 循环等待回调函数
    rospy.spin()


if __name__ == '__main__':
    main()
